﻿using System;
using System.Collections.Generic;
using Engine;
using Game;
using Mekiasm.Common;
using Mekiasm.ElectricElement;

namespace Mekiasm
{
    public class MetalFomer : MachineRotateAbleItem,Interface.IDynamicCraft
    {
        public static int Index;

        public int[] FuelCraftSlotIndies => new int[0];

        public int[] CraftSlotIndies => new int[] { 0};

        public int[] ResultSlotIndies => new int[] { 1};

        public int[] RemainSlotIndies => new int[0];

        public int SlotsCount => 2;

        public int BlockValue => blockValue;

        public Point2 CraftSize => new Point2(1);

        public MetalFomer() : base("金属成型机")
        {
            textureSlots = new int[] { 129, 130, 130, 130, 131, 128 };
        }

        public override MekBlock.DeviceType GetDeviceType()
        {
            return MekBlock.DeviceType.Electric;
        }

        public override bool OnInteract(SubsystemBlockBehavior blockBehavior, TerrainRaycastResult raycastResult, ComponentMiner componentMiner)
        {
            ComponentMetalFormer mcomponent = ILibrary.SubsystemBlockEntities.GetBlockEntity(raycastResult.CellFace.X, raycastResult.CellFace.Y, raycastResult.CellFace.Z).Entity.FindComponent<ComponentMetalFormer>(true);
            componentMiner.ComponentPlayer.ComponentGui.ModalPanelWidget = new MetalFormerWidget(mcomponent, componentMiner);
            return true;
        }

        public override void OnBlockAdded(SubsystemBlockBehavior blockBehavior, int value, int oldValue, int x, int y, int z)
        {
            ILibrary.AddBaseEnergyEntity(DisplayName, x, y, z, null,
                ILibrary.GetRotetedConfig(value, new MachineFaceConfig[] {
            new MachineFaceConfig(MekBlock.MachineMode.IN,MekBlock.DeviceType.Electric),
            new MachineFaceConfig(MekBlock.MachineMode.IN,MekBlock.DeviceType.Electric),
            new MachineFaceConfig(MekBlock.MachineMode.IN,MekBlock.DeviceType.Electric),
            new MachineFaceConfig(MekBlock.MachineMode.IN,MekBlock.DeviceType.Electric),
            new MachineFaceConfig(MekBlock.MachineMode.IN,MekBlock.DeviceType.Item),
            new MachineFaceConfig(MekBlock.MachineMode.OUT,MekBlock.DeviceType.Item)
            }), new MachineAmountLimitConfig() { MaxPower = 10240, OutputPowerLimit = 32, InputPowerLimit = 32 });
            ElectricSystem.Add(new MetalFormerElement(x, y, z));
        }

        public override void OnBlockGenerated(int value, int x, int y, int z, bool isLoaded)
        {
            ElectricSystem.Add(new MetalFormerElement(x, y, z));
        }

        public override void OnBlockRemoved(SubsystemBlockBehavior blockBehavior, int value, int newValue, int x, int y, int z)
        {
            ILibrary.RemoveBlockEntity(x, y, z);
            ElectricSystem.Remove(new Point3(x, y, z));
        }

        public void GenerateRecipes(List<MekRecipe> mekRecipe)
        {
            mekRecipe.Add(new MekRecipe(1).SetName("铁板").SetIngredian(0, ILibrary.GetCraftId(IronIngotBlock.Index)).SetResult(0, IronPlate.Index));
            mekRecipe.Add(new MekRecipe(1).SetName("铜板").SetIngredian(0, ILibrary.GetCraftId(CopperIngotBlock.Index)).SetResult(0, CopperPlate.Index));
            mekRecipe.Add(new MekRecipe(1).SetName("锡板").SetIngredian(0, ILibrary.GetCraftId(TinIngnot.Index)).SetResult(0, TinPlate.Index));
            mekRecipe.Add(new MekRecipe(1).SetName("青铜板").SetIngredian(0, ILibrary.GetCraftId(BronzeIngot.Index)).SetResult(0, BronzePlate.Index));
            mekRecipe.Add(new MekRecipe(1).SetName("钢板").SetIngredian(0, ILibrary.GetCraftId(SteelIgnot.Index)).SetResult(0, SteelPlate.Index));

            mekRecipe.Add(new MekRecipe(1).SetName("铁质外壳").SetIngredian(0, ILibrary.GetCraftId(IronPlate.Index)).SetResult(0, IronShell.Index).SetCraftMode(1));
            mekRecipe.Add(new MekRecipe(1).SetName("铜质外壳").SetIngredian(0, ILibrary.GetCraftId(CopperPlate.Index)).SetResult(0, CopperShell.Index).SetCraftMode(1));
            mekRecipe.Add(new MekRecipe(1).SetName("锡质外壳").SetIngredian(0, ILibrary.GetCraftId(TinPlate.Index)).SetResult(0, TinShell.Index).SetCraftMode(1));
            mekRecipe.Add(new MekRecipe(1).SetName("青铜质外壳").SetIngredian(0, ILibrary.GetCraftId(BronzeIngot.Index)).SetResult(0, BronzeShell.Index).SetCraftMode(1));
            mekRecipe.Add(new MekRecipe(1).SetName("铜质导线").SetIngredian(0, ILibrary.GetCraftId(CopperShell.Index)).SetResult(0, CopperCable.Index).SetCraftMode(1));
            mekRecipe.Add(new MekRecipe(1).SetName("锡质导线").SetIngredian(0, ILibrary.GetCraftId(TinShell.Index)).SetResult(0, TinCable.Index).SetCraftMode(1));

            mekRecipe.Add(new MekRecipe(1).SetName("铜质导线").SetIngredian(0, ILibrary.GetCraftId(CopperIngotBlock.Index)).SetResult(0, CopperCable.Index, 3).SetCraftMode(2));
            mekRecipe.Add(new MekRecipe(1).SetName("锡质导线").SetIngredian(0, ILibrary.GetCraftId(TinIngnot.Index)).SetResult(0, TinCable.Index, 3).SetCraftMode(2));

        }

        public void GenerateRecipeWidget(CanvasWidget canvas, ComponentEnergyMachine energyMachine, ComponentMiner miner, ref Action Tick)
        {
            var panelHori1 = new StackPanelWidget() { Direction = LayoutDirection.Horizontal, HorizontalAlignment = WidgetAlignment.Center, VerticalAlignment = WidgetAlignment.Center };
            var panelVerti1 = new StackPanelWidget() { Direction = LayoutDirection.Vertical, VerticalAlignment = WidgetAlignment.Center, Margin = new Vector2(10, 0) };
            var progress = new MekProgressWidget(MekProgressWidget.ProgessType.metal_former) { Size = new Vector2(128, 22), Margin = new Vector2(10) };
            var buttonContainer = new CanvasWidget() { Size = new Vector2(32f),HorizontalAlignment=WidgetAlignment.Center };
            var button = new BevelledRectangleWidget() { BevelSize = 2f };
            var click = new ClickableWidget() { SoundName ="Audio/Click"};
            var modeImage = new BlockIconWidget();
            var craft = new InventorySlotWidget() { Size = new Vector2(58f) };
            var result = new InventorySlotWidget() { Size = new Vector2(58f) };
            buttonContainer.AddChildren(button);buttonContainer.AddChildren(modeImage);buttonContainer.AddChildren(click);
            panelVerti1.AddChildren(progress);panelVerti1.AddChildren(buttonContainer);
            craft.AssignInventorySlot(energyMachine,0);
            result.AssignInventorySlot(energyMachine,1);
            panelHori1.AddChildren(craft);panelHori1.AddChildren(panelVerti1);panelHori1.AddChildren(result);
            canvas.AddChildren(panelHori1);
            Tick += () => {
                switch (energyMachine.CraftModeType)
                {
                    case 0:
                        modeImage.Value = ForgingHammer.Index;
                        break;
                    case 1:
                        modeImage.Value = Cutter.Index;
                        break;
                    case 2:
                        modeImage.Value = CopperCable.Index;
                        break;
                }
                if (click.IsClicked && !(energyMachine is ComponentCraftDemoMachine))
                {
                    energyMachine.CraftModeType++;
                    if (energyMachine.CraftModeType == 3) energyMachine.CraftModeType = 0;
                    energyMachine.CurrentRecipe = null;
                }
                progress.SetProgress(energyMachine.CraftProgress);
            };
        }
    }
}
